<template>
    <el-card>
        <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
            <el-descriptions :column="3" border>
                <el-descriptions-item label="客户"> <el-select v-model="formData.kehu_id" filterable remote
                        reserve-keyword placeholder="输入关键词搜索" remote-show-suffix :remote-method="remoteMethod"
                        :loading="loading" @change="getaddress">
                        <el-option v-for="item in kehu" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select></el-descriptions-item>
                <el-descriptions-item label="收货地址">

                    <el-select v-model="formData.address_id" clearable placeholder="请选择">
                        <el-option v-for="item in addressslist" :key="item.id" :label="item.name + item.address"
                            :value="item.id" />
                    </el-select>
                </el-descriptions-item>
                <el-descriptions-item label="制单"><el-input readonly v-model="formData.create" /></el-descriptions-item>
                <el-descriptions-item label="客户订单号"> <el-input v-model="formData.orderNo" clearable
                        placeholder="请输入客户订单号" /></el-descriptions-item>
                <el-descriptions-item label="交货时间"> <el-date-picker class="flex-1 !flex" v-model="formData.de_time"
                        clearable type="date" value-format="YYYY-MM-DD" placeholder="选择交货时间">
                    </el-date-picker></el-descriptions-item>
                <el-descriptions-item label="备注"> <el-input v-model="formData.remark" clearable
                        placeholder="请输入备注" /></el-descriptions-item>
            </el-descriptions>
        </el-form>
    </el-card>
    <el-card class="!border-none"  shadow="never">
            <el-button  type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
            </el-button>
            <el-button  :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button>
            <el-button v-perms="['orders.orders/add']" type="danger" @click="handleSubmit">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                保存
            </el-button>
            <div class="mt-4">
             <el-form ref="detailRef" :model="detail" label-width="90px" >
            <el-table :data="detail" border @selection-change="handleSelectionChange" >
                    <el-table-column type="selection" width="55"  />
                    <el-table-column label="商品名称" prop="name" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-input v-model="row.name" clearable placeholder="品名" />
                        </template>
                    </el-table-column>
                    <el-table-column label="产品类型" prop="protype">
                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.protype" clearable placeholder="产品类型">
                        <el-option 
                            v-for="(item, index) in dictData.protype"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="规格" prop="guige" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-input v-model="row.guige" clearable placeholder="规格" />
                        </template>
                    </el-table-column>
                    <el-table-column label="下单数量" prop="num" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-input v-model="row.num" clearable placeholder="数量" />
                        </template>
                    </el-table-column>
                  
                    <el-table-column label="单价" prop="price"  show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-input v-model="row.price" clearable placeholder="单价" />
                        </template>
                    </el-table-column>

                    <el-table-column label="单位" prop="unit">
                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.unit" clearable placeholder="单位">
                        <el-option 
                            v-for="(item, index) in dictData.unit"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                        </template>
                    </el-table-column>

                    <el-table-column label="物料编号" prop="wuliaoNo" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-input v-model="row.wuliaoNo" clearable placeholder="请输入物料编号" />
                        </template>
                    </el-table-column>
                    <el-table-column label="图纸" prop="tuzhi">
                        <template #default="{ row }">
                            <material-picker v-model="row.tuzhi" />
                        </template>
                    </el-table-column>
                    <el-table-column label="合计" prop="price" v-if="lists" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ row.price * row.num }}
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-input v-model="row.remark" clearable placeholder="备注" />
                        </template>
                    </el-table-column>
                </el-table>
                </el-form>
            </div>
   </el-card>
</template>
<script lang="ts" setup name="ordersAdd">
import useUserStore from "@/stores/modules/user";
import { timeFormat } from "@/utils/util";
import { apiKehuLists } from '@/api/kehu/kehu'
import { apiKehuAddressLists } from '@/api/kehu/address'
import { useDictData } from '@/hooks/useDictOptions'
import { apiOrdersAdd } from "@/api/orders/orders";
const userStore = useUserStore();
const emit = defineEmits(["success", "close"]);
// 表单数据
const formData = reactive({
    id: "",
    kehu_id: "",
    create: userStore.userInfo.name,
    orderNo: "",
    create_time: timeFormat(new Date().getTime(), 'yyyy-mm-dd hh:MM:ss'),
    de_time: "",
    remark: "",
    address_id: "",
});
// 表单数据
const detail = reactive([
{
    idx:0,
    name: '',
    guige: '',
    num: '',
    unit: '',
    price: '',
    protype:'',
    wuliaoNo: '',
    tuzhi: '',
    remark: ''
}
])
const formRules = reactive<any>({});
const loading = ref(false)
const list = ref<any>([])
const { dictData } = useDictData('unit,protype')
let kehu = ref<any>([])
let addressslist = ref<any[]>([])
// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
   
    selectData.value = val.map(({idx}) => idx);
};
onMounted(() => {
    apiKehuLists({ page_size: 999, page_no: 1 }).then(res => {
        list.value = res.lists
    })

})
const getaddress = async (id: number) => {
    addressslist.value = []
    formData.address_id = ''
    await apiKehuAddressLists({
        kehu_id: id
    }).then(res => {
        addressslist.value = res.lists
    })

}
const handleAdd = async ()=>{
    detail.push({
    idx:detail.length,
    name: '',
    guige: '',
    num: '',
    unit: '',
    protype:'',
    price: '',
    wuliaoNo: '',
    tuzhi: '',
    remark: ''
})
}
const handleDelete = async(ids:any)=>{
  
    ids.map((item: any)=>{
        console.log(item)
        detail.splice(item,1)
    })
  
}
const remoteMethod = (query: string) => {
    if (query) {
        loading.value = true
        setTimeout(() => {
            loading.value = false
            kehu.value = list.value.filter((item: { name: string; }) => {
                return item.name.toLowerCase().includes(query.toLowerCase())
            })
        }, 200)
    } else {
        kehu.value = []
    }
}
const handleSubmit = async()=>{
   await apiOrdersAdd({
        order:formData,
        detail:detail
    })
    emit("success");
}
</script>
